package org.siberia.client;

import java.util.HashMap;
import java.util.Map;

import org.siberia.client.action.EraseDataAction;
import org.siberia.client.action.FeedDataAction;
import org.siberia.client.service.TemplateEngineService;
import org.siberia.client.service.TemplateEngineServiceAsync;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Padding;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.CardLayout;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Gcalendar_tools implements EntryPoint,
									   ValueChangeHandler<String>
{
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final TemplateEngineServiceAsync greetingService = GWT.create(TemplateEngineService.class);
	
	private static final String WELCOME   = "welcome";
	private static final String TEMPLATES = "templates";
	private static final String TEMPLATE_FORM  = "template_form";
	private static final String ACTION_FORM = "action_form";
	
	/** map linking links and the relative view to display in the cardlayout */
	private Map<String, Component> cardComponentsMap = new HashMap<String, Component>(); 
	
	private CardLayout cardLayout = null;
	
	/** create stack panel
	 *  @return un StackPanel
	 */
	private LayoutContainer createActionPanel()
	{
		
		VBoxLayout layout = new VBoxLayout();
		layout.setPadding(new Padding(2));

		LayoutContainer c = new LayoutContainer(layout);
		
		Hyperlink linkAccueil = new Hyperlink("Accueil", WELCOME);
		c.add(linkAccueil);

		Hyperlink linkTemplates = new Hyperlink("Mes templates", TEMPLATES);
		c.add(linkTemplates);

		Hyperlink linkTemplateForm = new Hyperlink("template form", TEMPLATE_FORM);
		c.add(linkTemplateForm);

		Hyperlink linkActionForm = new Hyperlink("action form", ACTION_FORM);
		c.add(linkActionForm);
		
		Button feedDataButton = new Button("feed data", new SelectionListener<ButtonEvent>()
		{
			@Override
			public void componentSelected(ButtonEvent ce)
			{
				new FeedDataAction().run();
			}
		});
		c.add(feedDataButton);
		Button eraseDataButton = new Button("erase data", new SelectionListener<ButtonEvent>()
		{
			@Override
			public void componentSelected(ButtonEvent ce)
			{
				new EraseDataAction().run();
			}
		});
		c.add(eraseDataButton);
		
		return c;
	}
	
	@Override
	public void onValueChange(ValueChangeEvent<String> event)
	{
//		System.out.println("toto : " + event.getValue() + " (" + event.getType() + ")");
		if ( event != null && event.getValue() != null )
		{ 
			Component componentToSelect = this.cardComponentsMap.get(event.getValue());
			
			if ( componentToSelect != null )
			{
				this.cardLayout.setActiveItem(componentToSelect);
			}
		}
	}

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad()
	{	
		History.addValueChangeHandler(this);
		
		HBoxLayout horizontalLayout = new HBoxLayout(); 
		LayoutContainer c = new LayoutContainer(horizontalLayout);
		c.setSize(1000, 800);
		
		// action panel
		LayoutContainer actionPanel = this.createActionPanel();
		actionPanel.setSize(100, 800);
		c.add(actionPanel);
		
		// card layout
		this.cardLayout = new CardLayout();
		LayoutContainer cardContainer = new LayoutContainer(this.cardLayout);
		cardContainer.setSize(890, 800);
		
		// accueil
		Welcome welcome = new Welcome();
		cardContainer.add(welcome);
		cardComponentsMap.put(WELCOME, welcome);
		
		// templates
		TemplatesList templateList = new TemplatesList( 800, 770 );
	    templateList.setText("Mes templates");
		cardContainer.add(templateList);
		cardComponentsMap.put(TEMPLATES, templateList);
		templateList.setSize(890, 800);

		// template form
		ActionSuiteFormPanel actionSuiteForm = new ActionSuiteFormPanel();
		cardContainer.add(actionSuiteForm);
		//actionSuiteForm.setSize(800, 570);
		actionSuiteForm.setSize(800, 370);
		cardComponentsMap.put(TEMPLATE_FORM, actionSuiteForm);
		
		// action form
		ActionFormPanel actionForm = new ActionFormPanel();
		cardContainer.add(actionForm);
		cardComponentsMap.put(ACTION_FORM, actionForm);

		c.add(cardContainer);
		c.addStyleName("subContent");
		
		RootPanel.get("content").add(c);
	
		String Token = History.getToken();
  		if (Token.length() == 0)
  		{
  			History.newItem(WELCOME);
		}
		History.fireCurrentHistoryState();
	}
}
